package com.example.demo.rocketmq.filterSql;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class consumer {

    /**
     * sql过滤消费
     * sql过滤默认是关闭的，broker配置设置：enablePropertyFilter=true
     * @param args
     * @throws MQClientException
     */
    public static void main(String[] args) throws MQClientException {
        System.out.println("消费者启动开始");
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group");
        consumer.setNamesrvAddr("192.168.1.102:9876");
        //设值sql过滤条件
        MessageSelector selector=MessageSelector.bySql("age > 10 and age < 68");
        consumer.subscribe("topic1", selector);
        //单消息消费
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg : list) {
                    System.out.println("sql过滤的值："+msg.getUserProperty("age"));
                    System.out.println("消息：" + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
